/*
 * jxSwing Project (2012, 2013, 2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.text;

import java.awt.Dimension;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;

import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.date.DateHourFormatters;
import org.jxUtils.date.TimeFormatter;

/**
 * @author Denilson Edinaldo Pinto
 * @project jxSwing
 * @package org.jxSwing.component.text
 * @date 27/08/2012 - 20:50:29
 */
public final class DateField extends FormattedField {
	
	private static final long serialVersionUID = 1706248245647580753L;
	
	private static final DateHourFormatters FORMATTERS = new DateHourFormatters();
	
	private TimeFormatter dateFormatter = new TimeFormatter();
	
	/**
     * 
     */
	public DateField() {
	
		this(false);
	}
	
	/**
	 * @param required
	 */
	public DateField(final boolean required) {
	
		this(new TimeFormatter(), required);
	}
	
	/**
	 * @param pattern
	 * @param mask
	 * @param required
	 */
	public DateField(final String pattern, final String mask, final boolean required) {
	
		this(new TimeFormatter(pattern, mask), required);
	}
	
	/**
	 * @param dateFormatter
	 * @param required
	 */
	public DateField(final TimeFormatter dateFormatter, final boolean required) {
	
		super(dateFormatter.getMaskFormatter(), required);
		
		this.dateFormatter = dateFormatter;
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setName(I18N.component().date());
		setSize(new Dimension(80, 25));
		setPreferredSize(new Dimension(80, 25));
		setHorizontalAlignment(CENTER);
		
		//
		setInputVerifier(new InputVerifier() {
			
			@Override
			public boolean verify(final JComponent input) {
			
				//
				if(isEmpty()) {
					
					setValue(null);
					
					return true;
					
				}else {
					
					try {
						//
						final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(getPattern());
						
						simpleDateFormat.setLenient(false);
						simpleDateFormat.parse(getText());
						
						return true;
						
					}catch(final ParseException parseException) {
						
						JOptionPane.showMessageDialog(DateField.this, I18N.component().invalidValueDate(getText()));
						
						setValue(null);
						
						return false;
					}
				}
			}
		});
	}
	
	/**
	 * @return the {@link Calendar} or null
	 */
	public Calendar getCalendar() {
		
		Date date = getDate();
		
		// 
		if(date != null) {
			
			Calendar calendar = Calendar.getInstance();
			
			calendar.setTime(date);
			
			return calendar;
		}
		
		return null;
	}
	
	/**
	 * @return the {@link Date} or null
	 */
	public Date getDate() {
	
		//
		if(isEmpty()) {
			
			return null;
			
		}else {
			
			try {
				//
				return new SimpleDateFormat(getPattern()).parse(getText());
				
			}catch(final ParseException parseException) {
				
				parseException.printStackTrace();
				
				return null;
			}
		}
	}
	
	/**
	 * @return the dateFormatter
	 */
	public TimeFormatter getDateFormatter() {
	
		return dateFormatter;
	}
	
	/**
	 * @return the {@link java.sql.Date} or null
	 */
	public java.sql.Date getDateSQL() {
		
		Date date = getDate();
		
		return date != null ? new java.sql.Date(date.getTime()) : null;
	}
	
	/**
	 * @return the maskFormatter
	 */
	public MaskFormatter getMaskFormatter() {
	
		return getDateFormatter().getMaskFormatter();
	}
	
	/**
	 * @return the pattern
	 */
	public String getPattern() {
	
		return getDateFormatter().getPattern();
	}
	
	/**
	 * @return the {@link Time} or null
	 */
	public Time getTime() {
		
		Date date = getDate();
		
		return date != null ? new Time(date.getTime()) : null;
	}
	
	/**
	 * @return the {@link Timestamp} or null
	 */
	public Timestamp getTimestamp() {
		
		Date date = getDate();
		
		return date != null ? new Timestamp(date.getTime()) : null;
	}
	
	/**
	 * @return {@code getText().replaceAll("\\D", "").trim().isEmpty()}
	 */
	public boolean isEmpty() {
	
		return getText().replaceAll("\\D", "").trim().isEmpty();
	}
	
	/**
	 * @param date
	 */
	public void setDate(final Date date) {
	
		final String strNow = date != null ? new SimpleDateFormat(getPattern()).format(date) : null;
		
		setText(strNow);
		setValue(strNow);
		
		updateUI();
	}
	
	/**
	 * @param date
	 */
	public void setDateText(final String date) {
	
		// 
		if(CheckUtil.isNullOrAbsoluteEmpty(date)) {
			
			setDate(null);
			
		}else {
			
			//
			for(final String pattern : FORMATTERS.getPatterns()) {
				
				try {
					//
					setDate(new SimpleDateFormat(pattern).parse(date));
					
					break;
					
				}catch(final ParseException parseException) {
					// ignore
				}
			}
		}
	}
	
	/**
	 * 
	 */
	public void setNow() {
	
		setDate(new Date());
	}
	
	/**
	 * 
	 */
	public void validateDate() {
	
		boolean dateIsValid = true;
		
		//
		if(isEmpty()) {
			
			dateIsValid = false;
			
		}else {
			
			try {
				//
				final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(getPattern());
				
				simpleDateFormat.setLenient(false);
				simpleDateFormat.parse(getText());
				
			}catch(final ParseException parseException) {
				
				dateIsValid = false;
			}
		}
		
		//
		if( !dateIsValid) {
			
			setNow();
		}
	}
}
